package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Contact;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;


public class ContactDao {
	
	private DBHelp<Contact> db = new DBHelp<Contact>();
	
	public void save (Contact ct){
		String sql = "insert into t_contact(id,`name`,companyname,mobile,tel,address,mail,`index`,weibo,content,createtime,projectid)value(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, ct.getId(),ct.getName(),ct.getCompanyname(),ct.getMobile(),ct.getTel(),ct.getAddress(),ct.getMail(),ct.getIndex(),ct.getWeibo(),ct.getContent(),ct.getCreatetime(),ct.getProjectid());
	}
	public void update(Contact ct){
		String sql = "update t_contact set `name`=?,companyname=?,mobile=?,tel=?,address=?,mail=?,`index`=?,weibo=?,projectid=?,content=? where id=?";
		db.executeSQL(sql, ct.getId(),ct.getName(),ct.getCompanyname(),ct.getMobile(),ct.getTel(),ct.getAddress(),ct.getMail(),ct.getIndex(),ct.getWeibo(),ct.getProjectid(),ct.getContent());
	}
	public void del(String id){
		String sql = "delete from t_contact where id=? ";
		db.executeSQL(sql, id);
	}
	public Contact findById(String id ){
		String sql = "select id,`name`,companyname,mobile,tel,address,mail,`index`,weibo,content,createtime,projectid from t_contact";
		return db.executeQueryToObject(sql, new CtRowMapper(), id);
	}
	public Contact findByName(String name){
		String sql="select id,`name`,companyname,mobile,tel,address,mail,`index`,weibo,content,createtime,projectid from t_contact";
		return db.executeQueryToObject(sql, new CtRowMapper(), name);
	}
	
	public List<Contact> findByProject(String pid){
		String sql = "select id ,`name`,companyname,mobile,tel,address,mail,`index`,weibo,content,createtime,projectid from t_contact where projectid=?";
		return db.executeQueryToList(sql, new CtRowMapper(),pid);
		
	} 
	
	public class CtRowMapper implements RowMapper<Contact>{

		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact ct = new Contact();
			
			ct.setId(rs.getString("id"));
			ct.setName(rs.getString("name"));
			ct.setCompanyname(rs.getString("companyname"));
			ct.setAddress(rs.getString("address"));
			ct.setMobile(rs.getString("mobile"));
			ct.setTel(rs.getString("tel"));
			ct.setMail(rs.getString("mail"));
			ct.setIndex(rs.getString("index"));
			ct.setWeibo(rs.getString("weibo"));
			ct.setContent(rs.getString("content"));
			ct.setProjectid(rs.getString("projectid"));
			ct.setCreatetime(rs.getString("createtime"));
			return ct;
		}
		
		
	}

	public List<Contact> findAll() {
		String sql="select id,name,conpanyname,address,mobile,tel,mail,index,content,projectid,createtime from t_contact";
		
		return db.executeQueryToList(sql, new CtRowMapper());
	}
	
	
	
	
	
	
	
	
	

}
